/**
 * Account JavaScript Library
 * @author ReGeDa
 */

// update captcha image
function updateCaptcha() {

	var wait;

	var pic = $("captcha-pic");

	// update image
	pic.loadImg("/captcha.png", {
		before: function() {
			wait = jWait.micro(pic.parent()).css({
				position: "absolute",
				left: 0,
				top:0
			});
		},
		after: function() {
			wait.die();
		}
	});
}

// create a new account
function accountCreate(f) {
	
	var input = $(f);

	var rules = {
		fields: {
			email: "required : email",
			secret: "required : pass_strength",
			verify: "required : match[f:secret] : noQuery",
			username: "required : min_len[s:3] : pattern[s:[a-z][a-z\\d\\-]+]",
			captcha: "required : exact_len[s:3] : numeric",
			agree: "required : noQuery"
		},
		translateFields: jLang.form.fields
	};

	if (input.validate(rules)) {
		$.AJAX("/account/create/json", {
			oncomplete: function(j, status) {
				// OK
				if (status == 200) {
					if (j.code) {
						// show backend error
						switch(j.code.intval()) {
							case 23505: alert(jLang.uniqueViolation[j.err]); break;
						}
					}
					else if (j.err)  {
						// show error message
						alert(j.err);
					}
					else {

						var mailAddress = f.elements["email"].value;
						var mailServer = mailAddress.substr(mailAddress.indexOf('@') + 1);

						input.parent().html(
							"<h1>" + jLang.congratulations + "!</h1><h2>" + jLang.successReg
							+ "</h2><p>" + jLang.verifyMail.format([mailServer, mailAddress])
							+ "</p><p>" + jLang.gotoHome.format([j.id]) + "</p>"
						);

						// exit
						return;
					}
					// update a captcha
					updateCaptcha();
				}
			},
			query: input.query()
		});
	}

	return false;
}

// validate a user account
function accountLogin(f) {
	
	var input = $(f), e = $("id");

	// by EMAIL
	if (e.is_email())
		e.attr("name", "email");
	// by USERNAME
	else if (/^[a-z][a-z\d\-]+$/.test(e.val()))
		e.attr("name", "username");
	// no set...
	else {
		alert(jLang.printID);
		e.attr("name", "id").context().focus();
		return false;
	}

	// set rules
	var rules = {
		fields: {
			secret: "required"
		},
		translateFields: jLang.form.fields
	};


	if (input.validate(rules)) {
		$.AJAX("/account/login/json", {
			oncomplete: function(j, status) {
				// OK
				if (status == 200) {
					if (j.err)  {
						// show error message
						alert(j.err);
					}
					else if ($.cake.get("id") == j.id) {
						window.location = '/' + j.name;
					}
				}
			},
			query: input.query()
		});
	}

	return false;
}

// view password strength
function viewPasswdStrength(e) {
	var level = $(e).passwdStrength();
	var label = $("passwd-strength");
	if (level) {
		// strength colors
		var color = ["f53212","f59812","1282f5","5c9d07","047019"];
		// show mark
		label.css("color", '#' + color[level - 1]).html(jLang.form.passwdStrength[level - 1]);
	}
	else
		label.flush();
}

/**
 * User Picture Proccessor
 * @author ReGeDa
 */
upic = {
	// start upload
	upload: function(e) {
		e.form.submit();
	},
	// stop upload
	process: function(args) {
		if (args.err)
			alert(args.err);
		else {
			$(":userpic").first().loadImg(args.url);
		}
	}
}

// end of file